package com.epfresh.supplier.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.supplier.R;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.Address;
import com.epfresh.supplier.entity.ContentEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*已停用，暂时自提点功能不提供选择，只有一个为自动选取*/
public class OrderSelectPickAddressActivity extends BaseActivity {
    private ListView mListView;
    private List<Address> mDataList;
    private String pickupId="";
    String accountId = "";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_select_pickup);
        mListView = (ListView)findViewById(R.id.listview);
        mContext = this;
        pickupId = getIntent().getStringExtra("pickupId");
        basetitle.setText("选择自提点");
        baseback.setVisibility(View.VISIBLE);
        tvBack.setVisibility(View.VISIBLE);
        rightbtn.setVisibility(View.GONE);
        mHttpLoad();
    }

    OnRequestListener<ContentEntity<Address>> onRequestListener = new OnRequestListener<ContentEntity<Address>>() {
        @Override
        public ContentEntity<Address> jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, new TypeToken<ContentEntity<Address>>() {
            }.getType());
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {

        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<ContentEntity<Address>> response, Object tag) {
            ContentEntity<Address> addresses = response.getResponseElement();
            mDataList = addresses.getContent();
            mListView.setAdapter(new CommonAdapter<Address>(mContext, mDataList,
                    R.layout.item_pickup_select) {
                @Override
                public void convert(final ViewHolder holder, final Address bean) {
                    holder.setText(R.id.recevier_name, bean.getAddress() +" "+ bean.getName());
                    holder.setImageResource(R.id.select, R.mipmap.order_select_);
                    if (bean.getId().equals(pickupId)) {
                        holder.setImageResource(R.id.select, R.mipmap.order_select);
                    }
                    holder.setOnClickListener(R.id.select_ll, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            v.getId();
                            Intent reback = new Intent();
                            reback.putExtra("pickupAddress", bean.getAddress() +" "+ bean.getName());
                            reback.putExtra("pickupId", bean.getId());
                            setResult(2, reback);
                            finish();
                        }
                    });
                }

            });
        }

    };

    /**
     * 加载提货地址列表数据
     */
    private void mHttpLoad() {

        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();

/*        SharedPreferences user = getSharedPreferences(
                Constant.USERINFO, Activity.MODE_PRIVATE);
        String token = user.getString("token", "");*/
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_pickUpAddress_list);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> map = new HashMap<>();
        //data.setToken(token);
        map.put("accountId", getUser().getAccountId());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_store_store_pickUpAddress_list, onRequestListener);
    }


}
